<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.jpg" alt="logo" />
        <span>海报设计系统</span>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            prefix-icon="el-icon-unlock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%;" @click="doSubmit">
            登录
          </el-button>
        </el-form-item>
        <el-row style="text-align: center; margin-top: -10px">
          <el-link type="primary" @click="register">
            没有账号？点击注册
          </el-link>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
      },
    };
  },
  methods: {
    register() {
      this.$router.push('/register');
    },
    doSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          axios.post('/api/login', this.loginForm).then((response) => {
            if (response.data.success) {
              console.log("登录成功");
              this.$router.replace('/panel');
            } else {
              console.log("账号或密码错误");
              this.$message.error(response.data.message);
            }
          });
        } else {
          console.log("表单验证失败");
        }
      });
    },
  },
};
</script>

<style scoped>
/* 样式保持不变 */
</style>
